const { app, BrowserWindow } = require('electron')
const path = require('path')
const isDev = require('electron-is-dev')

async function createWindow() {
  const mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false
    }
  })

  const url = isDev
    ? 'http://localhost:3000'
    : `file://${path.join(__dirname, '../out/index.html')}`

  try {
    await mainWindow.loadURL(url)
    if (isDev) {
      mainWindow.webContents.openDevTools()
    }
  } catch (error) {
    console.error('Failed to load URL:', error)
  }
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})